function plotOri(ROIactivity, ids, varargin)

x_label = 0;
x_sym_strings = [];
tracecolor = '';
plottype = 'timecourse';
newfigurehandle = 1;
fontsize = 10;
lim_y = 0;
smoothit = 0;

if ~isempty(varargin)
    numIndex = find(cellfun('isclass', varargin(1:end-1), 'char'));
    for ind = 1:length(numIndex)
        switch lower(varargin{numIndex(ind)})
            case 'plottype'
                plottype = varargin{numIndex(ind) + 1};
            case 'smooth'
                smoothit = varargin{numIndex(ind) + 1};
            case 'newfigurehandle'
                newfigurehandle = varargin{numIndex(ind) + 1};
            case 'x_label'
                x_label = varargin{numIndex(ind) + 1};
            case 'x_label_string'
                x_sym_strings = varargin{numIndex(ind) + 1};
            case 'color'
                tracecolor = varargin{numIndex(ind) + 1};
            case 'fontsize'
                fontsize = varargin{numIndex(ind) + 1};
            case 'lim_y'  % [min max]
                lim_y = varargin{numIndex(ind) + 1};
        end
    end
end

% reshape the boundaries and ensure that on and off periods are always the same
% if not clip off the first off and/ or the last on frames
dim = size(ids.stim_boundaries);
ids.stim_boundaries = reshape(ids.stim_boundaries, dim(1) * dim(2), dim(3));
% mean activity for tuningcurve
for ind = 1:size(ids.stim_boundaries,1)
    ac_max(ind) = mean(ROIactivity(ids.stim_boundaries(ind, 2):ids.stim_boundaries(ind, 3)));
end
ac_max =  reshape(ac_max, dim(1), dim(2));
min_period = min(diff(ids.stim_boundaries, [], 2));
ids.stim_boundaries(:,1) = ids.stim_boundaries(:, 2) - min_period(1);
ids.stim_boundaries(:,3) = ids.stim_boundaries(:, 2) + min_period(2);

% generate the indices
stim_ids = [];
for ind = 1:dim(1)*dim(2)
    stim_ids = [stim_ids linspace(ids.stim_boundaries(ind, 1), ids.stim_boundaries(ind, 3), ...
        sum(min_period) + 1)];
end
stim_ids = reshape(stim_ids, sum(min_period) + 1, dim(1), dim(2));

IDs_plain = reshape(stim_ids,1,[]);
ac_plain = ROIactivity(IDs_plain);


switch plottype
    case 'timecourse'
        ac_temp = reshape(ac_plain,(sum(min_period) + 1) * dim(1), dim(2));
        
        % for the shading
        stim_on2 = ids.stim_on(IDs_plain);
        stim_on2 = stim_on2(1:(sum(min_period) + 1) * dim(1));
        
        if lim_y == 0
            min_y = min(min(ac_temp)) * 0.95;
            max_y = max(max(ac_temp)) * 1.05;
        else
            min_y = lim_y(1) * 0.95;
            max_y = lim_y(2) * 1.05;
        end
        
        if newfigurehandle
            h = figure;
        end
        hold on
        area((stim_on2 > 0) * max_y, min_y-10, ...
            'EdgeColor', [0.7 0.7 0.7] , 'Facecolor', [0.7 0.7 0.7]);
        if smoothit
            size_ac = size(ac_temp);
            ac_temp = smooth(ac_temp(:));
            ac_temp = reshape(ac_temp,size_ac(1), size_ac(2));
        end
        if strcmp(tracecolor, 'color')
            plot(ac_temp, 'Color', [0.5 0.5 0.5], 'LineWidth', 2);
        else
            plot(ac_temp, 'LineWidth', 2);
        end
        
        if smoothit
            ac_mean = smooth(mean(ac_temp, 2),0.05,'sgolay');
        else
            ac_mean = mean(ac_temp, 2);
        end
        plot(ac_mean, 'LineWidth', 3, 'Color', 'k');
        ylim([min_y max_y]);
        xlim([1 size(ac_temp,1)]);
        set(gca, 'XTick', [], 'Fontsize', fontsize);
        plotpos = get(gca, 'Position');
%         plotpos([1,3]) = [0.05 0.94];
%         set(gca, 'Position', plotpos);
        
        % requires ori.ttf installed as font on your machine
        if x_label
            sym_step_size = plotpos(3) / dim(1) * 0.5;
            x_sym = linspace(plotpos(1), plotpos(1) + plotpos(3), dim(1)+1) + sym_step_size;
            if isempty(x_sym_strings)
                switch dim(1)
                    case 8
                        x_sym_strings = 'VXJLNPRT';
                    case 16
                        x_sym_strings = 'VWXYJKLMNOPQRSTU';
                end
                for ii = 1:dim(1)
                    annotation('textbox',[x_sym(ii) 0 0.1 0.1], 'String', x_sym_strings(ii), ...
                        'FontName', 'OriSymbols', 'FontSize',25,'LineStyle', 'none')
                end
            end
        end
        
    case 'tuningpolar'
        figure;
        hh = polar(unique(ids.stimseq_deg(ids.stimseq_deg > -1))*pi/180, mean(ac_max,2)');
        patch( get(hh,'XData'), get(hh,'YData'), 'b' )
        view(-180,-90)
        set(gcf, 'Renderer', 'zbuffer')
    case 'tuningflat'
        figure;
        hold on
        plot(ac_max);
        plot(mean(ac_max, 2), 'LineWidth', 2, 'Color', 'k');
        hold off
end

